package tk.uodrad.apex.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.uodrad.apex.project.ex.ServiceException;
import tk.uodrad.apex.project.pojo.dto.CommunityAddNewDTO;
import tk.uodrad.apex.project.pojo.dto.CommunityUpdateDTO;
import tk.uodrad.apex.project.pojo.entity.Community;
import tk.uodrad.apex.project.service.CommunityService;
import tk.uodrad.apex.project.mapper.CommunityMapper;
import org.springframework.stereotype.Service;
import tk.uodrad.apex.project.utils.InitRandomCode;
import tk.uodrad.apex.project.web.ServiceCode;

/**
* @author Administrator
* @description 针对表【community_management】的数据库操作Service实现
* @createDate 2023-04-06 19:10:41
*/
@Service
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Community> implements CommunityService {

    @Autowired
    private CommunityMapper communityMapper;

    /**
     * 添加单个小区
     * @param communityAddNewDTO 添加小区DTO
     */
    @Override
    public void save(CommunityAddNewDTO communityAddNewDTO) {
        //新建一个Community对象并将传入的DTO复制给Community
        Community community = new Community();
        BeanUtils.copyProperties(communityAddNewDTO, community);
        //创建MyBatis-plus提供的Wrappers类下的query()方法
        QueryWrapper<Community> wrapper = Wrappers.query();
        //用eq方法用于判断数据库中是否有name等于当前Community的name
        wrapper.eq("name", community.getName());
        Community selectOne = communityMapper.selectOne(wrapper);
        if (selectOne != null) {
            String message = "新增数据失败，当前数据已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        //生成编号
        community.setCode(InitRandomCode.initCode("cm"));
        //插入数据，并判断是否插入成功
        int rows = communityMapper.insert(community);
        if (rows != 1) {
            String message = "服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    /**
     * 根据ID修改小区
     * @param communityUpdateDTO 修改小区DTO
     */
    @Override
    public void updateById(CommunityUpdateDTO communityUpdateDTO) {
        //根据传入的DTO中的ID查询数据库中是否存在该数据
        Community communityById = communityMapper.selectById(communityUpdateDTO.getId());
        if (communityById == null) {
            String message = "修改数据失败，当前数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        //创建一个Community对象，将传入的DTO复制给Community对象
        Community community = new Community();
        BeanUtils.copyProperties(communityUpdateDTO, community);
        //更新数据，并判断是否更新成功
        int rows = communityMapper.updateById(community);
        if (rows != 1) {
            String message = "服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public IPage<Community> list(Integer pageNum, Integer pageSize) {
        Page<Community> page = new Page<>(pageNum,pageSize);
        return communityMapper.selectPage(page,null);
    }
}




